package picasa;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gdata.client.Query;
import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.Kind.AdaptorException;
import com.google.gdata.data.Link;
import com.google.gdata.data.OtherContent;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.GphotoEntry;
import com.google.gdata.data.photos.GphotoFeed;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.data.photos.TagEntry;
import com.google.gdata.data.photos.UserFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ContentType;
import com.google.gdata.util.ServiceException;

public class PicasaTest {

	private static final String API_PREFIX = "https://picasaweb.google.com/data/feed/api/user/";

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String albumUrl = API_PREFIX + "default";
		PicasawebService service = new PicasawebService("exampleClient");
		service.useSsl();
		
		try {
			service.setUserCredentials("hiroa2", "parrramattta");
		} catch (AuthenticationException e2) {
			e2.printStackTrace();
		}

		UserFeed userFeed = null;
		try {
			userFeed = getFeed(service, albumUrl, UserFeed.class);
		} catch (IOException | ServiceException e1) {
			e1.printStackTrace();
			return;
		}

		List<GphotoEntry> entries = userFeed.getEntries();
		List<AlbumEntry> albums = new ArrayList<AlbumEntry>();
		for (GphotoEntry entry : entries) {
			GphotoEntry adapted;
			try {
				adapted = entry.getAdaptedEntry();
				if (adapted instanceof AlbumEntry) {
					albums.add((AlbumEntry) adapted);
				}
			} catch (AdaptorException e) {
				e.printStackTrace();
				return;
			}
		}
		
		AlbumEntry itpa = null;
		for (AlbumEntry ae : albums) {
			System.out.println(ae.getName());
			System.out.println(ae.getId());
			System.out.println(ae.getDescription().getPlainText());
			System.out.println(ae.getTitle().getPlainText());
			if (ae.getId().equals("https://picasaweb.google.com/data/entry/user/110612877692059971851/albumid/5703227923482620593")) {
				itpa = ae;
			}
		}
		
		String feedHref = null;
	    for (Link link : itpa.getLinks()) {
	        if (Link.Rel.FEED.equals(link.getRel())) {
	        	feedHref = link.getHref();
	        }
	      }
	    
	    System.out.println("feedHref: " + feedHref);;
	    AlbumFeed albumFeed = null;
		try {
			albumFeed = service.getFeed(new URL(feedHref), AlbumFeed.class);
		} catch (IOException | ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	    List<GphotoEntry> pe = albumFeed.getEntries();
	    List<PhotoEntry> photos = new ArrayList<PhotoEntry>();
	    for (GphotoEntry entry : pe) {
	      GphotoEntry adapted = null;
		try {
			adapted = entry.getAdaptedEntry();
		} catch (AdaptorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	      if (adapted instanceof PhotoEntry) {
	        photos.add((PhotoEntry) adapted);
	      }
	    }
	    
	    Link link = null;
		for (PhotoEntry pe2 : photos) {
			System.out.println(pe2.getGphotoId());
			System.out.println(pe2.getId());
			System.out.println(pe2.getDescription().getPlainText());
			System.out.println(pe2.getTitle().getPlainText());
			link = pe2.getEditLink();
		}
		
		try {
			PhotoEntry pengEnt = service.getEntry(new URL(link.getHref()), PhotoEntry.class);
			System.out.println("Can I get ? " + pengEnt.getGphotoId());
		} catch (IOException | ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		PhotoEntry newEntry = new PhotoEntry();
	    newEntry.setTitle(new PlainTextConstruct("新しいの"));
	    newEntry.setDescription(new PlainTextConstruct("説明"));
	    newEntry.setTimestamp(new Date());

	    OtherContent content = new OtherContent();


	    File file = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg");
	    byte[] result = new byte[(int) file.length()];
	    try {
			new FileInputStream(file).read(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	    content.setBytes(result);
	    content.setMimeType(new ContentType("image/jpeg"));
	    newEntry.setContent(content);
		
	    PhotoEntry updEntry = null;
		try {
			updEntry = service.insert(new URL(feedHref), newEntry);
		} catch (IOException | ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	    System.out.println("Uploaded Entry");
		System.out.println(updEntry.getGphotoId());
		System.out.println(updEntry.getFeedLink());
		System.out.println(updEntry.getDescription().getPlainText());
		System.out.println(updEntry.getTitle().getPlainText());
		System.out.println(updEntry.getMediaThumbnails().get(1).getUrl());
		
		TagEntry myTag = new TagEntry(); 
		myTag.setTitle(new PlainTextConstruct("penpen"));
		String editLink = updEntry.getFeedLink().getHref();

		try {
			service.insert(new URL(editLink), myTag);
		} catch (IOException | ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		Query myQuery = null;
		try {
			myQuery = new Query(new URL(feedHref.split("\\?")[0]));
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		myQuery.setStringCustomParameter("kind", "photo");
		myQuery.setStringCustomParameter("tag", "penpen");

		AlbumFeed searchResultsFeed = null;
		try {
			searchResultsFeed = service.query(myQuery, AlbumFeed.class);
		} catch (IOException | ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (PhotoEntry photo : searchResultsFeed.getPhotoEntries()) {
		    System.out.println(photo.getTitle().getPlainText());
		    try {
				photo.delete();
			} catch (IOException | ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	
	}

	/**
	 * Helper function to allow retrieval of a feed by string url, which will
	 * create the URL object for you. Most of the Link objects have a string
	 * href which must be converted into a URL by hand, this does the
	 * conversion.
	 */
	public static <T extends GphotoFeed> T getFeed(PicasawebService service,
			String feedHref, Class<T> feedClass) throws IOException,
			ServiceException {
		System.out.println("Get Feed URL: " + feedHref);
		return service.getFeed(new URL(feedHref), feedClass);
	}
}
